//
//  HMMyDisease.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/9/14.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftForms
import SwiftyJSON

class HMMyDisease: FormViewController {
    /// 疾病数据列表
    var diseaseList: JSON?   //Dictionary<String, Any>?
    /// 用户疾病列表
    var myDiseaseList: JSON?
    /// 选择的ID 列表
    var choiceList = Array<Any>()
   /// 添加导航栏右 按钮
    func addRBtn() {
        let but = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 38))
        but.setTitle("保存设置", for: .normal)
        but.setTitleColor(configure.shareConfigure.HMColor1, for: .normal)
        but.addTarget(self, action: #selector(save), for: UIControlEvents.touchUpInside)
        let itemR = UIBarButtonItem.init(customView: but)
        self.navigationItem.rightBarButtonItem = itemR
    }
    @objc func save() {
        NetRequestTool.shareInstance.met = .post
        NetRequestTool.requestForJSON(url: "user/illness", parameters: ["illness": self.choiceList], success: { (json) in
            
            self.navigationController?.popViewController(animated: true)
        }, fail: { (error, msg) in
            
        }) {}
        
        //
//        let alertController = UIAlertController(title: "Form output", message: message, preferredStyle: .alert)
//
//        let cancel = UIAlertAction(title: "OK", style: .cancel) { (action) in
//        }
//
//        alertController.addAction(cancel)
        
//        self.present(alertController, animated: true, completion: nil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
         addRBtn()
        self.title = "我的疾病"
        let form = FormDescriptor()
        form.title = "Example form"
        
        
        
        
        
        //第一个section分区
        let section1 = FormSectionDescriptor(headerTitle: nil, footerTitle: nil)
        section1.headerViewHeight = 0
        for (_,item) in diseaseList! {
            let id =  item["id"].stringValue
            let title = item["desc"].stringValue //item["desc"]
            let row = FormRowDescriptor(tag: id, type: .booleanCheck, title: title)
//            row.value = true as AnyObject
            row.configuration.button.didSelectClosure = { value in
                print("---->>>", Int(value.tag)!, value.title)

                self.choiceList.append(Int(value.tag)!)
            }
            
            if myDiseaseList?.isEmpty == false{
                for (_, myItem) in myDiseaseList! {
                    let choiceId = myItem["id"].stringValue
                    if id == choiceId {
                        row.value = true as AnyObject
                    }else {
                        row.value = false as AnyObject
                    }
                }
            }
            
            
            //        row2.value = true as AnyObject
            section1.rows.append(row)
//            i += 1
        }
        form.sections = [section1]
        self.form = form
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //登录按钮点击
    func submit() {
        //取消当前编辑状态
        self.view.endEditing(true)
        
        //将表单中输入的内容打印出来
        let message = self.form.formValues().description
        print(message)
    }
}



/// 自定义FormCell
